/**
 * @file
 * @auth jinguangguo
 * @date 2016/10/26
 */

import Vue from 'vue';
import moment from 'moment';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import tpl from './tpl.html';
import service from '../service';

export default Vue.extend({
    template: tpl,
    components: {
        loading: Loading,
        noData: NoData
    },
    data() {
        return {
            moment: moment,
            noticeId: this.$route.params.noticeId,
            isLoading: false,
            detail: null
        }
    },
    computed: {

    },
    created() {
        this.fetchData();
    },
    methods: {

        /**
         * 获取数据
         */
        fetchData() {
            this.isLoading = true;
            service.noticeDetail({
                noticeId: this.noticeId
            }).then(rep => {
                this.isLoading = false;
                let detail = rep.data;
                detail.contentHtml = getFormattedContentHtml(detail.content);
                this.detail = detail;
            });
        }

    }
});

function getFormattedContentHtml(content) {
    let arrs = content.split('\n');
    let results = [];
    $.map(arrs, (text, index) => {
        text = $.trim(text);
        results.push(`<p class="text">${text}</p>`);
    });
    return results.join('');
}